//
//  HasInstalledPowerTableViewCell2.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/31.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "HasInstalledPowerTableViewCell2.h"
#import "GetQRCodeViewController.h"

@implementation HasInstalledPowerTableViewCell2

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    UITapGestureRecognizer *tapRecognizerTel=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callFun)];
    
    _labelMerTel.userInteractionEnabled=YES;
    [_labelMerTel addGestureRecognizer:tapRecognizerTel];
    
    UITapGestureRecognizer *tapRecognizerAddress=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addresslFun)];
    
    _labelAddress.userInteractionEnabled=YES;
    [_labelAddress addGestureRecognizer:tapRecognizerAddress];
}

- (void)showDataWithModel:(HasInstalledModel *)model{
    model.shop = [[ShopModel alloc] init];
    [model.shop setValuesForKeysWithDictionary:model.store_info];
    
    _myModel = model;

    
    [_imageViewDianliangGreen removeFromSuperview];
    _imageViewDianliangGreen = nil;
    _imageViewDianliangGreen = [[UIImageView alloc] init];
    _imageViewDianliangGreen.backgroundColor = kGreenColor;
    [self addSubview:_imageViewDianliangGreen];
    
    //店名
    _labelShopName.text = model.shop.name;
    
    //老板
    _labelMerName.text = model.shop.contact;
    
    //老板联系方式
    _labelMerTel.text = model.shop.tel;
    
//    //电量
//    _labelPower.text = [NSString stringWithFormat:@"%ld%%",model.dump_energy.integerValue];
//    if (model.dump_energy.integerValue == 100) {
//        _imageViewPower.image = [UIImage imageNamed:@"lvdianliang"];
//        _imageViewDianliangGreen.hidden = YES;
//        _imageViewDianliangGreen.frame = CGRectMake(55.5, 109.5, 9, 14);
//        
//    }else{
////        double bili = model.dump_energy.doubleValue/100;
//        double bili = (15000 - 1000 * model.today_use_num.integerValue)/15000;
//        
//        _imageViewPower.image = [UIImage imageNamed:@"dianliang"];
//        _imageViewDianliangGreen.hidden = NO;
//        _imageViewDianliangGreen.frame = CGRectMake(55.5, 123.5 -(14 * bili) , 9, 14 * bili);
//    }
    
    _labelZongDian.text = @"15000mA";
    
    _labelHaodian.text = [NSString stringWithFormat:@"%ldmA",1000 * model.today_use_num.integerValue];
    
    
    //充电宝编号
    _labelNum.text = model.alias;
    
    //充电次数
    _labelTimes.text = [NSString stringWithFormat:@"%ld",model.total_charging_count.integerValue + model.today_use_num.integerValue];
    //今日充电次数
    _labelTimesToday.text = [NSString stringWithFormat:@"%ld",model.today_use_num.integerValue];
    
    //位置
    _labelAddress.text = [NSString stringWithFormat:@"%@%@",model.shop.area_name,model.shop.specific_address];
    
    //时间
//    if (model.on_line.integerValue == 0) {//不在线
//        _labelDate.text = model.offline_time;
//    }else{
//        _labelDate.text = model.online_time;
//    }
    
    NSString *dateString;
    if ([BGFunctionHelper isNULLOfString:model.last_use_time]) {
        dateString = @"该设备还未被使用";
    }else{
        dateString = model.last_use_time;
    }
    _labelDate.text = dateString;
    
    //涨粉数
    _labelZhangF.text = [NSString stringWithFormat:@"%ld (今日)",model.sub_count.integerValue];
}

- (IBAction)getButtonClick:(UIButton *)sender {
    
    NSString *wifiCode = @"https://www.wacdd.com/rd/device/";
    NSString *mimaCode = @"https://www.wacdd.com/rd/device_pwd/";
    NSString *resaultCode;
    
    if (_myModel.device_type.integerValue == 1) {
        resaultCode = [NSString stringWithFormat:@"%@%@",wifiCode,_myModel.alias];
    }else{
        resaultCode = [NSString stringWithFormat:@"%@%@",mimaCode,_myModel.alias];
    }
    
    UIStoryboard *storyboard = kMainStroyboard;
    GetQRCodeViewController *qrCodeVC = [storyboard instantiateViewControllerWithIdentifier:@"GetQRCodeViewController"];
    _myModel.shop = [[ShopModel alloc] init];
    [_myModel.shop setValuesForKeysWithDictionary:_myModel.store_info];
    qrCodeVC.model = _myModel.shop;
    qrCodeVC.alias = _myModel.alias;
    qrCodeVC.resaultCode = resaultCode;
    qrCodeVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [[self viewController] presentViewController:qrCodeVC animated:YES completion:nil];
}


- (UIViewController *)viewController {
    UIResponder *next = self.nextResponder;
    do {
        //判断响应者是否为视图控制器
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = next.nextResponder;
    } while (next != nil);
    
    return nil;
}

- (void)callFun{
    
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",_labelMerTel.text];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [[self viewController].view addSubview:callWebview];
}

- (void)addresslFun{
    [self alertVCByTitle:_labelAddress.text];
}

- (void)alertVCByTitle:(NSString *)title{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    [alert addAction:action];
    [[self viewController] presentViewController:alert animated:YES completion:nil];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
